2000W / 48V LLC Battery-Charger  V1.00
xmc_uart.c
Go to the documentation of this file.
1 
60 /*********************************************************************************************************************
61  * HEADER FILES
62  *********************************************************************************************************************/
63 
64 #include <xmc_scu.h>
65 #include <xmc_uart.h>
66 
67 /*********************************************************************************************************************
68  * MACROS
69  *********************************************************************************************************************/
70 
71 #define XMC_UART_CH_OVERSAMPLING (16UL)
72 #define XMC_UART_CH_OVERSAMPLING_MIN_VAL (4UL)
73 
74 /*********************************************************************************************************************
75  * API IMPLEMENTATION
76  *********************************************************************************************************************/
77 
78 void XMC_UART_CH_Init(XMC_USIC_CH_t *channel, const XMC_UART_CH_CONFIG_t *const config)
79 {
80  uint32_t oversampling = XMC_UART_CH_OVERSAMPLING;
81 
82  /* USIC channel switched on*/
83  XMC_USIC_CH_Enable(channel);
84 
85  if(config->oversampling != 0U)
86  {
87  oversampling = (uint32_t)config->oversampling;
88  }
89 
90  /* Configure baud rate */
91  (void)XMC_USIC_CH_SetBaudrate(channel, config->baudrate, oversampling);
92 
93  /* Configure frame format
94  * Configure the number of stop bits
95  * Pulse length is set to 0 to have standard UART signaling,
96  * i.e. the 0 level is signaled during the complete bit time
97  * Sampling point set equal to the half of the oversampling period
98  * Enable Sample Majority Decision
99  * Enable Transfer Status BUSY
100  */
101  channel->PCR_ASCMode = (uint32_t)(((config->stop_bits - 1UL) << USIC_CH_PCR_ASCMode_STPB_Pos) |
102  (((oversampling >> 1UL) + 1UL) << USIC_CH_PCR_ASCMode_SP_Pos) |
105 
106  /* Set passive data level, high
107  Set word length. Data bits - 1
108  If frame length is > 0, frame_lemgth-1; else, FLE = WLE (Data bits - 1)
109  Transmission Mode: The shift control signal is considered active if it
110  is at 1-level. This is the setting to be programmed to allow data transfers */
111  channel->SCTR = (uint32_t)((((uint32_t)config->data_bits - 1UL) << USIC_CH_SCTR_WLE_Pos) |
113 
114  if (config->frame_length != 0U)
115  {
116  channel->SCTR |= (uint32_t)(((uint32_t)config->frame_length - 1UL) << USIC_CH_SCTR_FLE_Pos);
117  }
118  else
119  {
120  channel->SCTR |= (uint32_t)(((uint32_t)config->data_bits - 1UL) << USIC_CH_SCTR_FLE_Pos);
121  }
122 
123  /* Enable transfer buffer */
124  channel->TCSR = (0x1UL << USIC_CH_TCSR_TDEN_Pos) |
126 
127  /* Clear protocol status */
128  channel->PSCR = 0xFFFFFFFFUL;
129 
130  /* Set parity settings */
131  channel->CCR = (uint32_t)config->parity_mode;
132 }
133 
134 XMC_UART_CH_STATUS_t XMC_UART_CH_SetBaudrate(XMC_USIC_CH_t *const channel, uint32_t rate, uint32_t oversampling)
135 {
136  XMC_UART_CH_STATUS_t status;
137 
138  status = XMC_UART_CH_STATUS_ERROR;
139 
140  if ((rate <= (XMC_SCU_CLOCK_GetPeripheralClockFrequency() >> 2U)) && (oversampling >= XMC_UART_CH_OVERSAMPLING_MIN_VAL))
141  {
142  if (XMC_USIC_CH_SetBaudrate(channel, rate, oversampling) == XMC_USIC_CH_STATUS_OK)
143  {
144  status = XMC_UART_CH_STATUS_OK;
145  }
146  }
147  return status;
148 }
149 
150 void XMC_UART_CH_Transmit(XMC_USIC_CH_t *const channel, const uint16_t data)
151 {
152  /* Check FIFO size */
153  if ((channel->TBCTR & USIC_CH_TBCTR_SIZE_Msk) == 0UL)
154  {
155  /* Wait till the Transmit Buffer is free for transmission */
157  {
158  }
159 
160  /* Clear the Transmit Buffer indication flag */
162 
163  /*Transmit data */
164  channel->TBUF[0U] = data;
165  }
166  else
167  {
168  channel->IN[0U] = data;
169  }
170 }
171 
173 {
174  uint16_t retval;
175 
176  /* Check FIFO size */
177  if ((channel->RBCTR & USIC_CH_RBCTR_SIZE_Msk) == 0U)
178  {
179  retval = (uint16_t)channel->RBUF;
180  }
181  else
182  {
183  retval = (uint16_t)channel->OUTR;
184  }
185 
186  return retval;
187 }
188 
190 {
192 
193  if (((XMC_USIC_CH_GetTransmitBufferStatus(channel) & (uint32_t) XMC_USIC_CH_TBUF_STATUS_BUSY) != 0U) ||
195  {
196  status = XMC_UART_CH_STATUS_BUSY;
197  }
198  else
199  {
200  /* USIC channel in IDLE mode */
202  }
203  return status;
204 }
205 
206 void XMC_UART_CH_EnableEvent(XMC_USIC_CH_t *const channel, const uint32_t event)
207 {
208  channel->CCR |= (event&0x1fc00U);
209  channel->PCR_ASCMode |= (event&0xf8U);
210 }
211 
212 void XMC_UART_CH_DisableEvent(XMC_USIC_CH_t *const channel, const uint32_t event)
213 {
214  channel->CCR &= (uint32_t)~(event&0x1fc00U);
215  channel->PCR_ASCMode &= (uint32_t)~(event&0xf8U);
216 }
uint16_t XMC_UART_CH_GetReceivedData(XMC_USIC_CH_t *const channel)
Definition: xmc_uart.c:172
#define USIC_CH_PCR_ASCMode_SP_Pos
Definition: XMC1400.h:3091
uint32_t XMC_SCU_CLOCK_GetPeripheralClockFrequency(void)
Definition: xmc1_scu.c:792
__STATIC_INLINE void XMC_USIC_CH_SetMode(XMC_USIC_CH_t *const channel, const XMC_USIC_CH_OPERATING_MODE_t mode)
Definition: xmc_usic.h:2076
__IO uint32_t TBUF[32]
Definition: xmc_usic.h:523
__STATIC_INLINE XMC_USIC_CH_TBUF_STATUS_t XMC_USIC_CH_GetTransmitBufferStatus(XMC_USIC_CH_t *const channel)
Definition: xmc_usic.h:1182
#define USIC_CH_TCSR_TDEN_Pos
Definition: XMC1400.h:2993
void XMC_UART_CH_Transmit(XMC_USIC_CH_t *const channel, const uint16_t data)
Definition: xmc_uart.c:150
XMC_UART_CH_STATUS_t XMC_UART_CH_Stop(XMC_USIC_CH_t *const channel)
Definition: xmc_uart.c:189
__O uint32_t IN[32]
Definition: xmc_usic.h:534
#define XMC_UART_CH_OVERSAMPLING_MIN_VAL
Definition: xmc_uart.c:72
XMC_USIC_CH_STATUS_t XMC_USIC_CH_SetBaudrate(XMC_USIC_CH_t *const channel, uint32_t rate, uint32_t oversampling)
Definition: xmc_usic.c:120
__IO uint32_t CCR
Definition: xmc_usic.h:504
uint32_t baudrate
Definition: xmc_uart.h:221
#define USIC_CH_TCSR_TDSSM_Msk
Definition: XMC1400.h:2992
uint8_t data_bits
Definition: xmc_uart.h:222
#define USIC_CH_SCTR_TRM_Pos
Definition: XMC1400.h:2967
uint8_t frame_length
Definition: xmc_uart.h:224
#define USIC_CH_PCR_ASCMode_TSTEN_Msk
Definition: XMC1400.h:3098
uint8_t stop_bits
Definition: xmc_uart.h:226
__STATIC_INLINE void XMC_UART_CH_ClearStatusFlag(XMC_USIC_CH_t *const channel, const uint32_t flag)
Definition: xmc_uart.h:559
uint8_t oversampling
Definition: xmc_uart.h:227
enum XMC_UART_CH_STATUS XMC_UART_CH_STATUS_t
__I uint32_t RBUF
Definition: xmc_usic.h:516
#define USIC_CH_PCR_ASCMode_STPB_Pos
Definition: XMC1400.h:3077
void XMC_USIC_CH_Enable(XMC_USIC_CH_t *const channel)
Definition: xmc_usic.c:79
__I uint32_t OUTR
Definition: xmc_usic.h:531
__IO uint32_t SCTR
Definition: xmc_usic.h:494
__IO uint32_t TCSR
Definition: xmc_usic.h:495
#define USIC_CH_SCTR_PDL_Msk
Definition: XMC1400.h:2960
void XMC_UART_CH_DisableEvent(XMC_USIC_CH_t *const channel, const uint32_t event)
Definition: xmc_uart.c:212
#define USIC_CH_TBCTR_SIZE_Msk
Definition: XMC1400.h:3530
__IO uint32_t TBCTR
Definition: xmc_usic.h:526
__IO uint32_t RBCTR
Definition: xmc_usic.h:527
XMC_UART_CH_STATUS_t XMC_UART_CH_SetBaudrate(XMC_USIC_CH_t *const channel, uint32_t rate, uint32_t oversampling)
Definition: xmc_uart.c:134
#define USIC_CH_PCR_ASCMode_SMD_Msk
Definition: XMC1400.h:3076
__O uint32_t PSCR
Definition: xmc_usic.h:514
#define USIC_CH_SCTR_WLE_Pos
Definition: XMC1400.h:2971
XMC_USIC_CH_PARITY_MODE_t parity_mode
Definition: xmc_uart.h:228
#define USIC_CH_RBCTR_SIZE_Msk
Definition: XMC1400.h:3554
void XMC_UART_CH_EnableEvent(XMC_USIC_CH_t *const channel, const uint32_t event)
Definition: xmc_uart.c:206
#define USIC_CH_SCTR_FLE_Pos
Definition: XMC1400.h:2969
#define XMC_UART_CH_OVERSAMPLING
Definition: xmc_uart.c:71
#define USIC_CH_PCR_ASCMode_RSTEN_Msk
Definition: XMC1400.h:3096
void XMC_UART_CH_Init(XMC_USIC_CH_t *channel, const XMC_UART_CH_CONFIG_t *const config)
Definition: xmc_uart.c:78
__IO uint32_t PCR_ASCMode
Definition: xmc_usic.h:502
__STATIC_INLINE uint32_t XMC_UART_CH_GetStatusFlag(XMC_USIC_CH_t *const channel)
Definition: xmc_uart.h:536